/**
 @file ctc_flexe.h

 @date 2020-4-1

 @version v6.0

 This file contains Flex Ethernet related data structure, enum, macro.

*/

#ifndef _CTC_FLEXE_H
#define _CTC_FLEXE_H
#ifdef __cplusplus
extern "C" {
#endif
/****************************************************************
 *
 * Header Files
 *
 ***************************************************************/
#include "sal_types.h"
#include "common/include/ctc_const.h"

/****************************************************************
*
* Defines and Macros
*
****************************************************************/
/**
 @defgroup flexe FLEXE
 @{
*/

/**
 @brief define flexe phy
*/
struct ctc_flexe_phy_s
{
    uint8 serdes_cnt;           /**< [TMM] valid number of serdes_id array */
    uint16* serdes_id;          /**< [TMM] serdes id arrary. indicate which phys to binding */
};
typedef struct ctc_flexe_phy_s ctc_flexe_phy_t;

#define CTC_FLEXE_GRP_FLAG_MANUAL  0x1 /**< [TMM] Static configure mode, controller configure the FlexE mux and demux
                                                  configurations directly and instruct the two ends when to switch calendars */
/**
 @brief define flexe group
*/
struct ctc_flexe_grp_s
{
    uint32 flag;               /**< [TMM] Flexe group flag, refer to CTC_FLEXE_GRP_FLAG_xx */
    uint32 group_num;          /**< [TMM] Flexe group number, 1-0xFFFFE */
    ctc_flexe_phy_t phy;       /**< [TMM] Binding phys to the flexe group */
};
typedef struct ctc_flexe_grp_s ctc_flexe_grp_t;

/**
 @brief define flexe group property type
*/
enum ctc_flexe_grp_prop_e
{
    CTC_FLEXE_GRP_PROP_GRP_NUM,       /**< [TMM]  p_value: uint32. Flexe group Number, 1-0xFFFFE */
    CTC_FLEXE_GRP_PROP_PHY,           /**< [TMM]  p_value: ctc_flexe_phy_t. Binding or Unbind phys to flexe group, if serdes_cnt is 0, means unbind */
    CTC_FLEXE_GRP_PROP_CAL_SEL,       /**< [TMM]  p_value: uint32.  0: indicate calendar A valid, 1: indicate calendar B valid, */
    CTC_FLEXE_GRP_PROP_SWITCH_TRIGGER,       /**< [TMM]  p_value: uint32.  trigger CR switch, value not care, */
    CTC_FLEXE_GRP_PROP_SWITCH_MODE,   /**< [TMM]  p_value: uint32. 0: indicate dynmic switch mode, 1: indicate static switch mode */
    CTC_FLEXE_GRP_PROP_CR_TIMER,      /**< [TMM]  p_value: uint32. 0 means diable, if set, dynamic switch will time out after CR send without receive CA during the timer */
    CTC_FLEXE_GRP_PROP_SYNC_MSG_EN,   /**< [TMM]  p_value: uint32. synchronization messaging channel, 0: disable , 1: enable(default is enable)*/
    CTC_FLEXE_GRP_PROP_PAD_EN,        /**< [TMM]  p_value: uint32. whether to enable padding, 0: disable, 1: enable */ 
    CTC_FLEXE_GRP_PROP_MAX
};
typedef enum ctc_flexe_grp_prop_e  ctc_flexe_grp_prop_t;


#define CTC_FLEXE_INSTANCE_SLOT_NUM 80
#define CTC_FLEXE_OH_MF_NUM 32

typedef uint32 ctc_slot_bmp_t[((CTC_FLEXE_INSTANCE_SLOT_NUM + CTC_UINT32_BITS -1 ) / CTC_UINT32_BITS)];

/**
 @brief define flexe slot
*/
struct ctc_flexe_slot_bmp_s
{
    uint16 serdes_id;            /**< [TMM] Serdes id, indicate which phy to selcet */
    ctc_slot_bmp_t slot_bmp;     /**< [TMM] Slot bitmap for alloc in specified phy */
};
typedef struct ctc_flexe_slot_bmp_s ctc_flexe_slot_bmp_t;

/**
 @brief define flexe client slot
*/
struct ctc_flexe_client_slot_s
{
    uint8 phy_cnt;              /**< [TMM] Valid number of slot array */
    uint8 rsv;
    uint16 flag;                /**< [TMM]  bit0:If set, indicate modify client slot force
                                            bit1:If set, indicate only modify client rx direction slot, only usefull when bit0 is set
                                            bit2:If set, indicate only modify client tx direction slot, only usefull when bit0 is set */
    ctc_flexe_slot_bmp_t* slot; /**< [TMM] Slot array per-phy */
};
typedef struct ctc_flexe_client_slot_s ctc_flexe_client_slot_t;

/**
 @brief define flexe client
*/
struct ctc_flexe_client_s
{
    uint32 group_id;               /**< [TMM] Flexe group id */
    uint16 client_num;             /**< [TMM] Flexe Client Number, 1-0xFFFE */
    ctc_flexe_client_slot_t slot;  /**< [TMM] Alloc slot */
    uint32 tx_rx_enable;           /**< [TMM] Enable tx or rx. bit0:tx, bit1:rx */
};
typedef struct ctc_flexe_client_s ctc_flexe_client_t;

/**
 @brief define flexe client property type
*/
enum ctc_flexe_client_prop_e
{
    CTC_FLEXE_CLIENT_PROP_GROUP_ID,          /**< [TMM]  p_value: uint32, Flexe group id */
    CTC_FLEXE_CLIENT_PROP_CLIENT_NUM,        /**< [TMM]  p_value: uint16, Flexe Client Number, 1-0xFFFE */
    CTC_FLEXE_CLIENT_PROP_BINDING_PORT,      /**< [TMM]  p_value: uint32, binding gport for forwad table. 0xFFFFFFFF means unbinding */
    CTC_FLEXE_CLIENT_PROP_SLOT,              /**< [TMM]  p_value: ctc_flexe_client_slot_t */
    CTC_FLEXE_CLIENT_PROP_TX_RX,             /**< [TMM]  p_value: uint32, Enable tx or rx. bit0:tx, bit1:rx */
    CTC_FLEXE_CLIENT_PROP_SPEED,             /**< [TMM]  p_value: uint32, used to get flexe client current work speed, multiply calendar granularity(defaut is 5G) */
    CTC_FLEXE_CLIENT_PROP_MAC_EN,            /**< [TMM]  p_value: uint32, used to Config Client Mac enable or disable  */
    CTC_FLEXE_CLIENT_PROP_RX_RECOVER,        /**< [TMM]  p_value: uint32, used to recover rx slot, value not care */
    CTC_FLEXE_CLIENT_PROP_MAX
};
typedef enum ctc_flexe_client_prop_e  ctc_flexe_client_prop_t;

/**
 @brief define flexe phy timeslot working status
*/
struct ctc_flexe_phy_timeslot_s
{
    uint16 serdes_id;                      /**< [TMM] Serdes id, indicate which phy to selcet */
    uint16 slot_num;                       /**< [TMM] Timeslot number used for phy */
    uint32 client_id[CTC_FLEXE_INSTANCE_SLOT_NUM];  /**< [TMM] Client ID for every timeslot on phy */
};
typedef struct ctc_flexe_phy_timeslot_s ctc_flexe_phy_timeslot_t;

#define CTC_FLEXE_OH_FLAG_C    0x1   /**< [TMM] FlexE Overhead C bit is set */
#define CTC_FLEXE_OH_FLAG_CR   0x2   /**< [TMM] FlexE Overhead CR bit is set */
#define CTC_FLEXE_OH_FLAG_CA   0x4   /**< [TMM] FlexE Overhead CA bit is set */
#define CTC_FLEXE_OH_FLAG_SC   0x8   /**< [TMM] FlexE Overhead SC bit is set */
#define CTC_FLEXE_OH_FLAG_RPF  0x10   /**< [TMM] FlexE Overhead RPF bit is set */

/**
 @brief define flexe OH infor struct
*/
struct ctc_flexe_phy_oh_s
{
    uint8  instance_offset;  /**< [TMM] FlexE instance offset */
    uint8   dir;             /**< [TMM] 0: rx, 1:tx */

    uint32 group_num;        /**< [TMM] FlexE group num */
    uint32 instance_num;     /**< [TMM] FlexE instance num */
    uint8  flexe_map[CTC_FLEXE_OH_MF_NUM];           /**< [TMM] indicate which instances are members of this group*/
    uint16 flag;            /**< [TMM] FlexE flag in Overhead */
    uint16 client_bmp_a[CTC_FLEXE_INSTANCE_SLOT_NUM];   /**< [TMM] Indicate which Flexe Client is mapped into a given calendar in calendar A */
    uint16 client_bmp_b[CTC_FLEXE_INSTANCE_SLOT_NUM];   /**< [TMM] Indicate which Flexe Client is mapped into a given calendar in calendar B */
};
typedef struct ctc_flexe_phy_oh_s ctc_flexe_phy_oh_t;


/**
 @brief define flexe phy property type
*/
enum ctc_flexe_phy_prop_e
{
    CTC_FLEXE_PHY_PROP_PHY_NUM,          /**< [TMM] p_value: uint32, FlexE PHY number. 1~254 for 100GBASE-R; 1~126 for 200GBASE-R; 1~62 for 400GBASE-R */
    CTC_FLEXE_PHY_PROP_SLOT,             /**< [TMM] Used to get working timeslot information on PHY, refer to ctc_flexe_phy_timeslot_t*/
    CTC_FLEXE_PHY_PROP_OH_INFO,          /**< [TMM] Used to get OH ram information, refer to ctc_flexe_phy_oh_t*/
    CTC_FLEXE_PHY_PROP_CRC_CNT,          /**< [TMM] p_value: uint32, Used to get CRC count */
    CTC_FLEXE_PHY_PROP_MAX
};
typedef enum ctc_flexe_phy_prop_e  ctc_flexe_phy_prop_t;

/**
 @brief define flexe client cross connect
*/
struct ctc_flexe_cross_s
{
    uint32  client_id;        /**< [TMM] Source flexe client */
    uint32  dest_client_id;   /**< [TMM] Dest flexe client */
    uint8   enable;           /**< [TMM] Enable cross connect */
};
typedef struct ctc_flexe_cross_s ctc_flexe_cross_t;

/**
 @brief define flexe event type
*/
enum ctc_flexe_event_type_e
{
    CTC_FLEXE_EVENT_GRP_NUM_MISMATCH,   /**< [TMM] FlexE Group Number mismatch */
    CTC_FLEXE_EVENT_INST_NUM_MISMATCH,  /**< [TMM] FlexE Instance Number mismatch */
    CTC_FLEXE_EVENT_MAP_MISMATCH,       /**< [TMM] FlexE Map mismatch */
    CTC_FLEXE_EVENT_CAL_MISMATCH,       /**< [TMM] FlexE client calendar mismatch */
    CTC_FLEXE_EVENT_RX_SWITCH_OK,       /**< [TMM] FlexE Calendar Rx negotiate ok, means receive C bit, and do rx switch */
    CTC_FLEXE_EVENT_TX_SWITCH_OK,       /**< [TMM] FlexE Calendar Tx negotiate ok, means send C bit */
    CTC_FLEXE_EVENT_RPF,                /**< [TMM] FlexE remote phy fault */
    CTC_FLEXE_EVENT_LOF,                /**< [TMM] FlexE loss of frame(ohlock) */
    CTC_FLEXE_EVENT_LOMF,               /**< [TMM] FlexE loss of multi-frame(ohlock) */
    CTC_FLEXE_EVENT_CRC_ERR,            /**< [TMM] FlexE FCS error */
    CTC_FLEXE_EVENT_PHY_DESKEW_OVER,    /**< [TMM] FlexE deskew over in Flexe PHY*/
    CTC_FLEXE_EVENT_GROUP_DESKEW_OVER,  /**< [TMM] FlexE deskew over in Flexe Group*/
    CTC_FLEXE_EVENT_GROUP_CR_TIMEOUT,   /**< [TMM] FlexE Group dynamic switch occur timeout event, refer to CTC_FLEXE_GRP_PROP_CR_TIMER property*/
    CTC_FLEXE_EVENT_LPF,                /**< [TMM] FlexE local phy fault */
    CTC_FLEXE_EVENT_SEND_CA,            /**< [TMM] Flexe Calendar Rx pretreatment finish, means send CA bit */
    CTC_FLEXE_EVENT_RCV_CA,             /**< [TMM] Flexe Calendar Tx switch begin, means receive CA bit, and do tx switch */

    CTC_FLEXE_EVENT_MAX
};
typedef enum ctc_flexe_event_type_e  ctc_flexe_event_type_t;

#define CTC_FLEXE_EVENT_FLAG_C_SET   0x1   /**< [TMM] FlexE Overhead C0 bit is set */
#define CTC_FLEXE_EVENT_FLAG_CR_SET  0x2   /**< [TMM] FlexE Overhead CR bit is set */
#define CTC_FLEXE_EVENT_FLAG_CA_SET  0x4   /**< [TMM] FlexE Overhead CA bit is set */
#define CTC_FLEXE_EVENT_FLAG_CLEAR   0x8   /**< [TMM] FlexE clear event occur */

/**
 @brief define flexe link change event
*/
struct ctc_flexe_link_event_s
{
    uint32 client_id;     /**< [TMM] client id, means which client occur flexe event */
    uint32 link_status;   /**< [TMM] 0: link up, 1: link down, 2: CCM(client calendar mismatch), 3: CCM recover */
};
typedef struct ctc_flexe_link_event_s ctc_flexe_link_event_t;

/**
 @brief define flexe miss event
*/
struct ctc_flexe_status_event_s
{
    uint8 event_type;       /**< [TMM] FlexE event bitmap, refer to ctc_flexe_event_type_t */
    uint8 rsv;
    uint16 serdes_id;       /**< [TMM] serdes id, means which phy occur FlexE event  */

    uint16 flag;            /**< [TMM] FlexE flag in Overhead */
    uint16 instance_offset; /**< [TMM] FlexE instance offset */
    uint32 group_num;       /**< [TMM] FlexE group num */
    uint32 instance_num;    /**< [TMM] FlexE instance num */
    uint8  flexe_map[CTC_FLEXE_OH_MF_NUM];           /**< [TMM] indicate which instances are members of this group*/
};
typedef struct ctc_flexe_status_event_s ctc_flexe_status_event_t;

/**@} end of @defgroup  flexe FLEXE */

#ifdef __cplusplus
}
#endif

#endif

